{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e0726a38",
   "metadata": {},
   "source": [
    "# 1.requests（让HTTPS服务人类）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "55f60dce",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:32.876179Z",
     "start_time": "2022-03-15T11:19:32.581030Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b9b7cb15",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:33.084288Z",
     "start_time": "2022-03-15T11:19:33.067345Z"
    }
   },
   "outputs": [],
   "source": [
    "## * r 是指 response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1d2c9995",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:36.004483Z",
     "start_time": "2022-03-15T11:19:33.623030Z"
    }
   },
   "outputs": [],
   "source": [
    "r = requests.get(\"https://cn.bing.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8edfb0e9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:36.066500Z",
     "start_time": "2022-03-15T11:19:36.040498Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dca04a83",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-08T12:44:01.669190Z",
     "start_time": "2022-03-08T12:44:01.650170Z"
    }
   },
   "source": [
    "## * r.content\n",
    "* 1. 可以获取请求数据内容（观察页面数据形态 HTML,Json）\n",
    "* 2. b 是指 bytes ； 二进制文件\n",
    "## *r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f31fe0a4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:37.396051Z",
     "start_time": "2022-03-15T11:19:36.266788Z"
    }
   },
   "outputs": [],
   "source": [
    "r = requests.get(\"http://httpbin.org/get\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "048119a7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:37.443065Z",
     "start_time": "2022-03-15T11:19:37.432062Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0b72f5a9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:37.912976Z",
     "start_time": "2022-03-15T11:19:37.899991Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\\n  \"args\": {}, \\n  \"headers\": {\\n    \"Accept\": \"*/*\", \\n    \"Accept-Encoding\": \"gzip, deflate\", \\n    \"Host\": \"httpbin.org\", \\n    \"User-Agent\": \"python-requests/2.25.1\", \\n    \"X-Amzn-Trace-Id\": \"Root=1-62307646-407dc436026d259a75f20997\"\\n  }, \\n  \"origin\": \"183.42.45.174\", \\n  \"url\": \"http://httpbin.org/get\"\\n}\\n'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "04671e64",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:38.585049Z",
     "start_time": "2022-03-15T11:19:38.576045Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.25.1',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-62307646-407dc436026d259a75f20997'},\n",
       " 'origin': '183.42.45.174',\n",
       " 'url': 'http://httpbin.org/get'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()  # 将json数据转换成字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d6d0d2da",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:39.270638Z",
     "start_time": "2022-03-15T11:19:39.254633Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'183.42.45.174'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['origin']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "583b676d",
   "metadata": {},
   "source": [
    "## 3.payload\n",
    "* 使用 payload 来实现请求数据传递 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fd7bbba8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:42.535645Z",
     "start_time": "2022-03-15T11:19:40.495044Z"
    }
   },
   "outputs": [],
   "source": [
    "payload = {\n",
    "    'name':'vin',\n",
    "    'job':'stu'\n",
    "}\n",
    "r = requests.get(\"https://httpbin.org/get\",params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "577205c1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:42.583664Z",
     "start_time": "2022-03-15T11:19:42.572659Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "83f8fd0d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:42.630674Z",
     "start_time": "2022-03-15T11:19:42.619668Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {'job': 'stu', 'name': 'vin'},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.25.1',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-6230764b-0f1f3c6f3192b91f0169e8ca'},\n",
       " 'origin': '183.42.45.174',\n",
       " 'url': 'https://httpbin.org/get?name=vin&job=stu'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c83dcfab",
   "metadata": {},
   "source": [
    "### face++ detect\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b8982a5e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:47.077459Z",
     "start_time": "2022-03-15T11:19:43.161361Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.准备 base url （对应该功能的url链接）\n",
    "face_detect_url=\"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "# 2.准备参数数据\n",
    "payload = {\n",
    "    'api_key': 'wH5jJXa327R2iOJ9TtgHkMXS4WHnfIzj',\n",
    "    'api_secret': 'APfd1pOaneXua_QcYfmcZEZrXfPfm1BM',\n",
    "    'image_url1':'https://tse3-mm.cn.bing.net/th/id/OIP-C._MVPR376nHT7sR7DbefPcAHaHa?pid=ImgDet&rs=1',\n",
    "    'return_landmark':1,\n",
    "    'return_attributes': 'gender,age,smiling,emotion'\n",
    "    \n",
    "}\n",
    "\n",
    "# 3.检查调用的请求方法：POST\n",
    "# 4.请求人脸数据\n",
    "r = requests.post(url = face_detect_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "67fdfe86",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:47.141498Z",
     "start_time": "2022-03-15T11:19:47.115445Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d26dc9e0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-15T11:19:47.221516Z",
     "start_time": "2022-03-15T11:19:47.194512Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647343182,4a497296-8675-4d72-8307-b9b3c003e393',\n",
       " 'time_used': 1879,\n",
       " 'error_message': 'MISSING_ARGUMENTS: face_token2, image_base64_2, image_url2, image_file2'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a6a3229",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a9f1ffa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
